package cn.pzhu.pserson.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.pzhu.pserson.dto.RulesDTO;
import cn.pzhu.pserson.util.ResponseUtil;
import cn.pzhu.pserson.service.IRulesService;
import cn.pzhu.pserson.dto.LimitShowDTO;
import cn.pzhu.pserson.model.Rules;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/rules")
public class RulesController {
    @Autowired
    private IRulesService rulesService;

    @RequestMapping(value = "/page/content", method = RequestMethod.GET)
    public ModelAndView list(Model model,
                             @Validated RulesDTO rulesDTO, BindingResult errors){

        Rules rule =  rulesService.findModel();

        model.addAttribute("content", rule);

        ModelAndView view = new ModelAndView("rules/add", (Map<String, ?>) model);
        return view;
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> rulesList = rulesService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
        long count = rulesService.count();

        result.put("rows", rulesList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @CrossOrigin
    @RequestMapping(value = "/getRule", method = RequestMethod.POST, produces = {"text/plain;charset=utf-8","text/html;charset=utf-8"}             )
    @ResponseBody
    public ResponseEntity<String> list(
            @Validated
                    LimitShowDTO limitShowDTO, BindingResult errors, HttpServletResponse response) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> rulesList = rulesService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
        long count = rulesService.count();

        result.put("content", rulesList.get(0).get("content"));

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<String> show() {
        Map<String, Object> rulesModel = rulesService.findMap();

        return ResponseUtil.getResEntityForGetAndJson(rulesModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        RulesDTO rulesDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Rules rulesModel = rulesDTO.toModel();

        return ResponseUtil.getResEntityForPPP(rulesService.save(rulesModel));
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseEntity<String> update(
        @Validated
        RulesDTO rulesDTO, BindingResult errors) {

        Rules rulesModel = rulesDTO.toModel();
        return ResponseUtil.getResEntityForPPP(rulesService.update(rulesModel));
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory() {
        return ResponseUtil.getResEntityForDel(rulesService.delete());
    }

}
